package com.resourcestore.action;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import com.resourcestore.dao.*;
import com.resourcestore.model.*;
import com.resourcestore.utils.CommonUtil;
import com.resourcestore.utils.tag.Pagination;

public class CourseTeaClassFKAction extends BaseAction {

	private Users users;
	private List<Users> listUsers;
	private List<String> listCheckedCourse = new ArrayList<String>();
	private UsersDao usersDao = new UsersDao();
	private Course course;
	private List<Course> listCourse;
	private CourseDao courseDao = new CourseDao();
	private CourseTeaClassFK courseTeaClassFK;
	private List<CourseTeaClassFK> listCourseTeaClassFK;
	private CourseTeaClassFKDao courseTeaClassFKDao = new CourseTeaClassFKDao();
	private Pagination pagination = new Pagination();
	private List<CourseTeaClassFK> listCourseTeaClassFKForm = new ArrayList<CourseTeaClassFK>();

	/**
	 * 查询返回List
	 * 
	 * @throws ParseException
	 * 
	 */
	public String listCourseTeaClassFK() {
		courseTeaClassFK = new CourseTeaClassFK();
		pagination.setTotal(courseTeaClassFKDao
				.queryCourseTeaClassFKNum(courseTeaClassFK));
		listCourseTeaClassFK = courseTeaClassFKDao.selectCourseTeaClassFKPage(
				courseTeaClassFK, pagination);
		// listCourseTeaClassFK =
		// dealCourCollection(listCourseTeaClassFK,courseDao);

		// 将bigint数字处理为时间格式

		for (int i = 0; i < listCourseTeaClassFK.size(); i++) {
			try {
				listCourseTeaClassFK.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourseTeaClassFK
								.get(i).getGmtCreated()));
				listCourseTeaClassFK.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourseTeaClassFK
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return "default";
	}

	/*
	 * 根据条件搜索，查询返回List带分页
	 * 
	 * @throws ParseException
	 */
	public String searchListCourseTeaClassFK() {

		// 对内容进行urldecoder解码
		try {
			courseTeaClassFK.setTeacherUsername(new String(courseTeaClassFK
					.getTeacherUsername().getBytes("iso-8859-1"), "UTF-8"));
			courseTeaClassFK.setCourseName(new String(courseTeaClassFK
					.getCourseName().getBytes("iso-8859-1"), "UTF-8"));
			courseTeaClassFK.setRealName(new String(courseTeaClassFK
					.getRealName().getBytes("iso-8859-1"), "UTF-8"));
			courseTeaClassFK.setClassName(new String(courseTeaClassFK
					.getClassName().getBytes("iso-8859-1"), "UTF-8"));
			courseTeaClassFK.setTermName(new String(courseTeaClassFK
					.getTermName().getBytes("iso-8859-1"), "UTF-8"));
			courseTeaClassFK.setAuthor(new String(courseTeaClassFK.getAuthor()
					.getBytes("iso-8859-1"), "UTF-8"));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		pagination.setTotal(courseTeaClassFKDao
				.queryCourseTeaClassFKNum(courseTeaClassFK));
		listCourseTeaClassFK = courseTeaClassFKDao.selectCourseTeaClassFKPage(
				courseTeaClassFK, pagination);

		// 将bigint数字处理为时间格式

		for (int i = 0; i < listCourseTeaClassFK.size(); i++) {
			try {
				listCourseTeaClassFK.get(i).setGmtCreatedString(
						CommonUtil.getSysTimeFormatString(listCourseTeaClassFK
								.get(i).getGmtCreated()));
				listCourseTeaClassFK.get(i).setGmtModifiedString(
						CommonUtil.getSysTimeFormatString(listCourseTeaClassFK
								.get(i).getGmtModified()));
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return newpath("/CourseTeaClassFK/listCourseTeaClassFK.jsp");
	}

	/**
	 * 处理RoleCollection字符串
	 * 
	 * @throws ParseException
	 * 
	 */
	// /*
	// * public List<CourseTeaClassFK> dealCourCollection(List<CourseTeaClassFK>
	// * list,CourseDao courseDao){ String str = null; String[] s = null; Course
	// * courTemp = new Course(); String sTemp = null; for (int i = 0; i <
	// * list.size(); i++) { try {
	// * list.get(i).setGmtCreatedString(CommonUtil.getSysTimeFormatString
	// * (list.get(i).getGmtCreated()));
	// * list.get(i).setGmtModifiedString(CommonUtil
	// * .getSysTimeFormatString(list.get(i).getGmtModified())); } catch
	// * (ParseException e) { // TODO Auto-generated catch block
	// * e.printStackTrace(); }
	// *
	// * str = list.get(i).getCourCollection(); if(str != null){ s =
	// * CommonUtil.split(str,","); sTemp = ""; for(int j=0;j<s.length;j++){
	// * courTemp.setCourseId(Integer.valueOf(s[j]));
	// * if(CommonUtil.isNotNullOrNotEqualsEmpty
	// * (courseDao.selectCourseById(courTemp))){ courTemp =
	// * courseDao.selectCourseById(courTemp); }else{
	// * courTemp.setCourseName("未知课程"); } sTemp = sTemp +
	// * " ["+courTemp.getCourseName()+"] "; }
	// * //list.get(i).setStringShowCourName(sTemp); }
	// *
	// * } return list; }
	// */
	/**
	 * 预处理插入信息
	 */
	/*
	 * public String preInsertCourseTeaClassFK() { listCourse =
	 * courseDao.selectCourse(); listUsers = usersDao.selectUsers();
	 * listCourseTeaClassFK = courseTeaClassFKDao.selectCourseTeaClassFK();
	 * 
	 * if(CommonUtil.isNotNullOrNotEqualsEmpty(listCourseTeaClassFK)){ //int s =
	 * listUsers.size(); for (int m =0; m < listUsers.size(); m++) { for (int n
	 * = 0; n < listCourseTeaClassFK.size(); n++) { if
	 * (listUsers.get(m).getUserName()
	 * .equals(listCourseTeaClassFK.get(n).getTeacherUsername())) {
	 * listUsers.remove(m); m--; break; } } } }
	 * 
	 * 
	 * for (int i = 0; i < listCourse.size(); i++) { try {
	 * listCourse.get(i).setCourseName(listCourse.get(i).getCourseName() + "("+
	 * listCourse.get(i).getCourseCateName() + ")");
	 * 
	 * } catch (Exception e) {
	 * 
	 * e.printStackTrace(); } }
	 * 
	 * for (int i = 0; i < listUsers.size(); i++) { try {
	 * listUsers.get(i).setRealName(listUsers.get(i).getRealName() + "("+
	 * listUsers.get(i).getUserName() +")");
	 * 
	 * } catch (Exception e) {
	 * 
	 * e.printStackTrace(); } }
	 * 
	 * return newpath("/CourseTeaClassFK/insertCourseTeaClassFK.jsp");
	 * 
	 * }
	 */

	/**
	 * 预插入一条数据
	 */
	public String preInsertCourseTeaClassFK() {

		return newpath("/CourseTeaClassFK/insertCourseTeaClassFK.jsp");
	}

	/**
	 * 插入一条数据
	 */
	public String insertCourseTeaClassFK() {

		for (int i = 0; i < listCourseTeaClassFKForm.size(); i++) {
			// 如果classId courseId termId teacherUsername同时有值才处理
			if (listCourseTeaClassFKForm.get(i).getClassId() != 0
					&& listCourseTeaClassFKForm.get(i).getCourseId() != 0
					&& listCourseTeaClassFKForm.get(i).getTermId() != 0
					&& CommonUtil
							.isNotNullOrNotEqualsEmpty(listCourseTeaClassFKForm
									.get(i).getTeacherUsername())) {
				listCourseTeaClassFKForm.get(i).setAuthor(courseTeaClassFK.getAuthor());
				CourseTeaClassFK courseTeaClassFKTemp = new CourseTeaClassFK();
				// 根据4个条件查询是否已存在记录
				courseTeaClassFKTemp = courseTeaClassFKDao
						.selectCourseTeaClassFKByFourCondition(listCourseTeaClassFKForm
								.get(i));
				if (!CommonUtil.isNotNullOrNotEqualsEmpty(courseTeaClassFKTemp)) {
					try {
						listCourseTeaClassFKForm.get(i).setGmtCreated(
								CommonUtil.getSysTimeFormatNum());
						listCourseTeaClassFKForm.get(i).setGmtModified(
								CommonUtil.getSysTimeFormatNum());
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					courseTeaClassFKDao
							.insertCourseTeaClassFK(listCourseTeaClassFKForm
									.get(i));
				}

			}
		}
		// 添加之前删除该教师数据
		// courseTeaClassFKDao.deleteCourseTeaClassFKByTUN(courseTeaClassFK);

		// String str = courseTeaClassFK.getCourseCollection();
		// if (str != null) {
		// String[] s = CommonUtil.split(str, ",");
		// for (int i = 0; i < s.length; i++) {
		// courseTeaClassFK.setCourseId(Integer.valueOf(s[i]));
		// courseTeaClassFKDao.insertCourseTeaClassFK(courseTeaClassFK);
		// }
		// }

		return redirect(this.getUrl());
	}

	/**
	 * 更新之前加载原信息
	 * 
	 */
	/*
	 * public String preUpdateCourseTeaClassFK() {
	 * 
	 * courseTeaClassFK =
	 * courseTeaClassFKDao.selectCourseTeaClassFKById(courseTeaClassFK);
	 * 
	 * String str = courseTeaClassFK.getCourCollection(); if(str != null){
	 * String[] s = CommonUtil.split(str, ","); for (int i = 0; i < s.length;
	 * i++) { listCheckedCourse.add(s[i]); } }
	 * 
	 * listCourse = courseDao.selectCourse(); for (int i = 0; i <
	 * listCourse.size(); i++) { try {
	 * listCourse.get(i).setCourseName(listCourse.get(i).getCourseName() + "("+
	 * listCourse.get(i).getCourseCateName()+ ")");
	 * 
	 * } catch (Exception e) {
	 * 
	 * e.printStackTrace(); } }
	 * 
	 * return newpath("/CourseTeaClassFK/updateCourseTeaClassFK.jsp"); }
	 */

	/**
	 * 更新信息
	 * 
	 */
	/*
	 * public String updateCourseTeaClassFK() {
	 * 
	 * try { //courseTeaClassFK.setGmtCreated(CommonUtil.getSysTimeFormatNum());
	 * courseTeaClassFK.setGmtModified(CommonUtil.getSysTimeFormatNum()); }
	 * catch (ParseException e) { // TODO Auto-generated catch block
	 * e.printStackTrace(); }
	 * 
	 * courseTeaClassFKDao.updateCourseTeaClassFK(courseTeaClassFK); return
	 * redirect(this.getUrl()); }
	 */

	/**
	 * 删除信息
	 */
	public String deleteCourseTeaClassFK() {
		courseTeaClassFKDao.deleteCourseTeaClassFK(courseTeaClassFK);
		return redirect(this.getUrl());
	}

	/**
	 * 批量删除信息
	 */
	public String deleteMoreCourseTeaClassFK() {
	
		courseTeaClassFKDao.deleteMoreCourseTeaClassFK(courseTeaClassFK);
		return redirect(this.getUrl());
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public CourseTeaClassFK getCourseTeaClassFK() {
		return courseTeaClassFK;
	}

	public void setCourseTeaClassFK(CourseTeaClassFK courseTeaClassFK) {
		this.courseTeaClassFK = courseTeaClassFK;
	}

	public List<CourseTeaClassFK> getListCourseTeaClassFK() {
		return listCourseTeaClassFK;
	}

	public void setListCourseTeaClassFK(
			List<CourseTeaClassFK> listCourseTeaClassFK) {
		this.listCourseTeaClassFK = listCourseTeaClassFK;
	}

	public Course getCourse() {
		return course;
	}

	public void setCourse(Course course) {
		this.course = course;
	}

	public List<Course> getListCourse() {
		return listCourse;
	}

	public void setListCourse(List<Course> listCourse) {
		listCourse = listCourse;
	}

	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}

	public List<Users> getListUsers() {
		return listUsers;
	}

	public void setListUsers(List<Users> listUsers) {
		listUsers = listUsers;
	}

	public List<String> getListCheckedCourse() {
		return listCheckedCourse;
	}

	public void setListCheckedCourse(List<String> listCheckedCourse) {
		this.listCheckedCourse = listCheckedCourse;
	}

	public List getListCourseTeaClassFKForm() {
		return listCourseTeaClassFKForm;
	}

	public void setListCourseTeaClassFKForm(
			List<CourseTeaClassFK> listCourseTeaClassFKForm) {
		this.listCourseTeaClassFKForm = listCourseTeaClassFKForm;
	}

}
